package com.example.bbsbe.controller;

import com.example.bbsbe.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


@RestController
@CrossOrigin(origins = "https://yushi.chat", allowCredentials = "true")
public class FileController {

    private final FileService fileService;

    @Autowired
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @PostMapping("/img/")
    public ResponseEntity<?> uploadImage(
            @RequestParam("file") MultipartFile file,
                @RequestHeader("username") String username) {

        Map<String, Object> response = fileService.saveImage(file, username);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/img2/")
    public ResponseEntity<?> uploadEditorImage(
            @RequestParam("wangeditor-uploaded-image") MultipartFile file,
            @RequestHeader("username") String username) {

        Map<String, Object> response = fileService.saveEditorImage(file, username);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/video/")
    public ResponseEntity<?> uploadEditorVideo(
            @RequestParam("wangeditor-uploaded-video") MultipartFile file,
            @RequestHeader("username") String username) {

        Map<String, Object> response = fileService.saveEditorVideo(file, username);
        return ResponseEntity.ok(response);
    }
}

